package com.libbase.extension

import android.content.pm.PackageManager
import android.graphics.drawable.Drawable
import android.os.Build

fun PackageManager.appVersion(packageName: String): String {
    val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getPackageInfo(
            packageName, PackageManager.PackageInfoFlags.of(0)
        )
    } else {
        @Suppress("DEPRECATION") getPackageInfo(packageName, 0)
    }

    return "v${
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            "${packageInfo.versionName}.${packageInfo.longVersionCode}"
        } else {
            @Suppress("DEPRECATION") "${packageInfo.versionName}.${packageInfo.versionCode}"
        }
    }"
}

fun PackageManager.appIcon(packageName: String): Drawable {
    val applicationInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
        getApplicationInfo(
            packageName, PackageManager.ApplicationInfoFlags.of(0)
        )
    } else {
        @Suppress("DEPRECATION") getApplicationInfo(packageName, 0)
    }

    return applicationInfo.loadIcon(this)
}